package com.sgb.link.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.link.BR;
import com.sgb.link.R;
import com.sgb.link.entity.MenuEntity;
import com.sgb.link.view.adapter.ItemViewHolder;
import com.sgb.link.view.adapter.PageRecycleAdapter;

import java.util.List;

public class MenuAdapter extends PageRecycleAdapter<MenuEntity> {

    private int resID;//本地资源id

    public MenuAdapter(Context context, List<MenuEntity> datas) {
        super(context, R.layout.layout_item_menu, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, MenuEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView iv = holder.itemView.findViewById(R.id.iv);
        ImageView iv_2 = holder.itemView.findViewById(R.id.iv_2);
        ImageView iv_3 = holder.itemView.findViewById(R.id.iv_3);

       /* String icon = entity.getUrl();
        //判断是否存在
        if (!TextUtils.isEmpty(icon)) {
            //获取到对应的本地图标地址id
            resID = context.getResources().getIdentifier(icon, "drawable", context.getPackageName());
        }
        //判断本地图标地址id是否找到
        if (resID != 0) {
            //Glide加载本地的图标图片iv
            Glide.with(context).load(resID).into(iv);
        } else {
            //Glide加载线上图标
            Glide.with(context).load(entity.getAppIcon()).into(iv);
        }
*/
        if (!TextUtils.isEmpty(entity.getMurl()) && entity.getMurl().contains("http")) {
            iv.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.VISIBLE);
//            ((TextView) holder.itemView.findViewById(R.id.tv)).setEllipsize(TextUtils.TruncateAt.END);
            Glide.with(context).load(entity.getMurl() + "")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.default_user))
                    .into(iv_3);
        } else if (entity.getAppIcon().contains("http")) {
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.GONE);
            iv.setVisibility(View.VISIBLE);

        } else {
            iv_3.setVisibility(View.GONE);
            iv_2.setVisibility(View.VISIBLE);
            iv.setVisibility(View.GONE);
        }

    }
}
